﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace Salon
{
    class SqlService
    {
        private SqlService()
        {

            // Pobranie connString
            FileStream fs = new FileStream("connection.txt", FileMode.Open, FileAccess.Read);
            try
            {
                StreamReader sr = new StreamReader(fs);
                connString = sr.ReadLine();
                sr.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Błąd odczytu \"connection.txt\" ");
            }
        }


        private static SqlService service = null;

        SqlConnection conn = null;

        //namiary na bazę są przechowywane w pliku app.config
        //String connString = ConfigurationSettings.AppSettings["ConnectionString"];

        // namiary na bazę są przechowywane w pliku connection.txt
        String connString = null;


        public static SqlService getInstance()
        {
            if (service == null)
                service = new SqlService();

            return service;
        }

        public SqlConnection getConnection()
        {
            if (conn == null)
                conn = new SqlConnection(connString);

            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();

            return conn;
        }

        /// <summary>
        /// Zwraca obiekt SqlCommand z instrukcją podaną w argumencie
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public SqlCommand getCommand(String query)
        {
            getConnection();
            return new SqlCommand(query, conn);
        }
    }
}
